// pages/details/index.js
import {
  updateList,
  getInfo
} from '../../api/index.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      date: '',
      content: '',
      time: '',
      title: '',
      _id: ''
    }
  },
  onLoad(option) {
    this.setData({
      ['form._id']: option.id || ''
    });
    const {
      type
    } = option;
    wx.setNavigationBarTitle({
      title: type == 'add' ? '新增内容' : '编辑内容'
    });
    this.getData();
  },
  async getData() {
    let res = await getInfo(this.data.form._id).then((res) => res).catch(err => err);
    this.setData({
      form: res.data || {}
    })
    console.log(res, '结果')
  },
  async formSubmit(e) {
    wx.showLoading({
      title: '提交中...',
      mask: true
    })
    await updateList(this.data.form).then(res => {
      getApp().globalData.refresh = true;
      wx.hideLoading();
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        success(){
          wx.redirectTo({
            url: '/pages/index/index',
          })
        }
      })
    }).catch((err) => {
      console.log(err)
      wx.hideLoading();
      wx.showToast({
        title: '提交失败',
        icon: 'error'
      })
    })
    console.log('提交', e.detail.value)
  },
  formReset(e) {
    console.log('重置', e.detail.value)
  },
  bindDateChange(e) {
    this.setData({
      ['form.date']: e.detail.value
    })
  },
  bindTimeChange(e) {
    this.setData({
      ['form.time']: e.detail.value
    })
  },
  bindTextAreaBlur(e) {
    this.setData({
      ['form.content']: e.detail.value
    })
  }
})